package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.CompanyAuthDao;
import com.leoman.entity.CompanyAuth;
import com.leoman.service.CompanyAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * CompanyAuthServiceImpl
 * Created by 涂奕恒 on 2016/10/18 0018 下午 3:22.
 */
@Service
public class CompanyAuthServiceImpl implements CompanyAuthService {

    @Autowired
    private CompanyAuthDao companyAuthDao;

    @Override
    public List<CompanyAuth> findAll() {
        return companyAuthDao.findAll();
    }

    @Override
    public Page<CompanyAuth> find(int pageNum, int pageSize) {
        return companyAuthDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<CompanyAuth> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public CompanyAuth getById(int id) {
        return companyAuthDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        companyAuthDao.delete(id);
    }

    @Override
    public CompanyAuth create(CompanyAuth companyAuth) {
        return companyAuthDao.save(companyAuth);
    }

    @Override
    public CompanyAuth update(CompanyAuth companyAuth) {
        return companyAuthDao.save(companyAuth);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public CompanyAuth findOneByCompanyUserId(Integer companyUserId) {
        return companyAuthDao.findOneByCompanyUserId(companyUserId);
    }
}